package com.fr.web.core.chwriter;

import com.fr.base.CodeUtils;
import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.Painter;
import com.fr.base.StringUtils;
import com.fr.base.Style;
import com.fr.base.core.html.Tag;
import com.fr.base.core.html.TextHtml;
import com.fr.base.core.json.JSONException;
import com.fr.data.core.db.BinaryObject;
import com.fr.report.CellElement;
import com.fr.report.ReportGetter;
import com.fr.report.cellElement.CellGUIAttr;
import com.fr.report.cellElement.Formula;
import com.fr.report.core.PaintUtils;
import com.fr.report.script.Calculator;
import com.fr.report.script.ParameterMapNameSpace;
import com.fr.report.script.core.parser.UtilEvalError;
import com.fr.report.web.ui.Widget;
import com.fr.report.web.ui.impl.form.TreeNodeToogleButton;
import com.fr.web.Repository;
import com.fr.web.core.CellBackgroundDimension;
import com.fr.web.core.HTMLWriterUtils;
import com.fr.web.core.SessionIDInfor;
import java.awt.Image;
import java.awt.Rectangle;
import java.sql.Blob;
import java.sql.Clob;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FormCellWriter extends TreeCellWriter
{
  private String fType = null;

  public FormCellWriter(Repository paramRepository, int paramInt)
  {
    super(paramRepository, paramInt);
    this.reportIndex = paramInt;
  }

  public FormCellWriter(Repository paramRepository, int paramInt, String paramString)
  {
    super(paramRepository, paramInt);
    this.fType = paramString;
  }

  public String getFType()
  {
    return this.fType;
  }

  public void setFType(String paramString)
  {
    this.fType = paramString;
  }

  public Tag cell2Tag(CellElement paramCellElement, ReportGetter paramReportGetter, Rectangle paramRectangle1, Rectangle paramRectangle2)
  {
    Tag localTag = null;
    try
    {
      localTag = super.cell2Tag(paramCellElement, paramReportGetter, paramRectangle1, paramRectangle2);
    }
    catch (JSONException localJSONException1)
    {
      FRContext.getLogger().log(Level.WARNING, localJSONException1.getMessage(), localJSONException1);
    }
    this.resTag.attr("id", ColumnRow.valueOf(paramCellElement.getColumn(), paramCellElement.getRow()) + "-" + this.reportIndex + "-" + this.tableID);
    if (this.cellHeight == 0)
      return this.resTag;
    Style localStyle = paramCellElement.getStyle();
    if (!(this.cellGUIAttr.isPreviewContent()))
    {
      HTMLWriterUtils.style_background2css(localStyle, this.resTag, this.cellGUIAttr, new CellBackgroundDimension(paramCellElement, paramReportGetter), this.repo);
      return this.resTag;
    }
    int i = 0;
    Object localObject1 = paramCellElement.getValue();
    if (localObject1 instanceof Formula)
    {
      localObject2 = Calculator.createCalculator();
      ((Calculator)localObject2).pushNameSpace(ParameterMapNameSpace.create(this.repo.getSessionIDInfor().getParameterMap4Execute()));
      try
      {
        localObject1 = ((Calculator)localObject2).eval(((Formula)localObject1).getContent().substring(1));
      }
      catch (UtilEvalError localUtilEvalError)
      {
        FRContext.getLogger().log(Level.WARNING, localUtilEvalError.getMessage(), localUtilEvalError);
      }
    }
    if (localObject1 instanceof Blob)
      localObject1 = dealWithValue4Blob(localObject1);
    if (localObject1 instanceof BinaryObject)
      localObject1 = dealBinaryObject(localObject1);
    if (localObject1 instanceof Clob)
      localObject1 = dealClobValue(localObject1);
    Object localObject2 = paramCellElement.getWidget();
    if (localObject2 != null)
    {
      String str = ((Widget)localObject2).getWidgetName();
      if (StringUtils.isNotBlank(str))
      {
        SessionIDInfor localSessionIDInfor = this.repo.getSessionIDInfor();
        if (localSessionIDInfor.getParameterValue(str) != null)
          localObject1 = localSessionIDInfor.getParameterValue(str);
      }
      if (localObject2 instanceof TreeNodeToogleButton)
        dealWithTreeNodeSonList((TreeNodeToogleButton)localObject2);
      try
      {
        this.resTag.attr("width", this.cellWidth + "");
        this.resTag.attr("height", this.cellHeight + "");
        this.resTag.attr("widget", widget2Config((Widget)localObject2, localObject1, ColumnRow.valueOf(paramCellElement.getColumn(), paramCellElement.getRow()), paramCellElement, this.reportIndex, this.fType));
        i = 1;
      }
      catch (JSONException localJSONException2)
      {
        FRContext.getLogger().log(Level.WARNING, localJSONException2.getMessage(), localJSONException2);
      }
    }
    if (i == 0)
      if ((localStyle.getVerticalText() == 1) || (localObject1 instanceof Painter) || (localObject1 instanceof Image))
      {
        HTMLWriterUtils.value2PaintableTag(localObject1, paramCellElement.getStyle(), this.cellWidth, this.cellHeight, localTag, this.repo, false);
        i = 1;
      }
      else if (localObject1 != null)
      {
        localTag.sub(new TextHtml(CodeUtils.htmlEncode(PaintUtils.valueToText(localObject1, paramCellElement.getStyle().getFormat()))));
      }
    if (i != 0)
      localStyle = localStyle.derivePadding(0, 0).deriveHorizontalAlignment(2);
    HTMLWriterUtils.style_background2css(localStyle, this.resTag, this.cellGUIAttr, new CellBackgroundDimension(paramCellElement, paramReportGetter), this.repo);
    return ((Tag)(Tag)this.resTag);
  }
}